package com.wf.course.web.controller.comm;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.utils.page.PageBean;
import com.wf.course.model.comm.ActivityNews;
import com.wf.course.service.IActivityNewsService;
import com.wf.course.web.Interceptor.LogInfo;

/**
 *活动信息管理
 */
@RequestMapping("activityNews")
@Controller
public class ActivityNewsController {

	@Autowired
	private IActivityNewsService activityNewsService;

	
	 /***
	  * 管理
	  * @param pageNo
	  * @param pageSize
	  * @param title
	  * @param model
	  * @return
	  */
	@LogInfo("查询活动信息列表")
	@RequestMapping("activityNewsList")
	public String activityNewsList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
						  @RequestParam(value = "pageSize", required = false) Integer pageSize,
						  @RequestParam(value = "title", required = false) String title,
 						  Model model){
		Map<String,Object> map=new HashMap<String,Object>(); 
		map.put("title",title);
 		
		PageBean<ActivityNews> pageBean = new PageBean<ActivityNews>(pageNo, pageSize,null,map);
		
		activityNewsService.getActivityNewsList(pageBean);
		pageBean.setUrl("/activityNews/activityNewsList");
		model.addAttribute("page", pageBean);
		
		return "comm/activityNewsList";
	}
	
	/**
	 * ：新增修改页面信息加载
	 * @param id
	 * @return
	 */
	@LogInfo("新增/修改活动信息，页面数据初始化")
	@RequestMapping("downLoadActivityNews/{id}")
	public String downLoadActivityNews(@PathVariable("id") Integer id,Model model){
		 if(id!=null && id!=0){
			 ActivityNews activityNews = this.activityNewsService.downLoadActivityNews(id);
				model.addAttribute("activityNews", activityNews);
				model.addAttribute("type", "upd");
		 }else{
			 model.addAttribute("type", "add");
		 }
		 
		return "comm/activityNewsEdit";		
	}
	
	
	/**
	 * 添加或修改
	 * @param activityNews
	 * @param request
	 * @return
	 */
	@LogInfo("新增/修改活动信息")
	@RequestMapping(value = {"activityNewsEdit" } )
	public String activityNewsEdit(@ModelAttribute ActivityNews activityNews, 
 			HttpServletRequest request){
		 
			//添加信息
			if(activityNews.getId()==null){				 
				//保存
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				 activityNews.setCreateTime(sdf.format(new Date()));
				 
				activityNewsService.saveActivityNews(activityNews);
			}else{
				//修改			
				activityNewsService.updateActivityNews(activityNews);				
			}		 
		 
		return "redirect:/activityNews/activityNewsList";
		
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除活动信息")
	@RequestMapping(value="deleteActivityNews/{id}"  )
	public String deleteActivityNews(@PathVariable Integer id){
			this.activityNewsService.deleteActivityNews(id);
			
			return "redirect:/activityNews/activityNewsList";
	}
	
}
